package com.mfoyou.agent.web.service.thirdPart;

import org.mfoyou.agent.common.dao.domain.HuawuAlipay;
import org.mfoyou.agent.utils.common.Logger;
import org.mfoyou.agent.utils.common.MfoyouMap;
import org.mfoyou.agent.utils.common.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;

@Service
public class AliPayService {

    private static String getWay = "https://openapi.alipay.com/gateway.do";
    private static Logger logger = Logger.getLogger(Logger.class);

    @Value("#{settings['alipay_notifyUrl']}")
    private String alipay_notifyUrl;

    @Deprecated
    public AlipayFundTransToaccountTransferResponse extract(int extractId, Integer amount, String name, String account, HuawuAlipay huawuAlipay) {
        if (huawuAlipay == null) return null;
        AlipayClient alipayClient = new DefaultAlipayClient(getWay, huawuAlipay.getAlipayAppid(), huawuAlipay.getAlipayPrivateKey(), "json", "utf-8", huawuAlipay.getAlipayPublicKey(), "RSA2");
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        MfoyouMap map = new MfoyouMap();
        map.put("out_biz_no", "" + extractId);
        map.put("payee_type", "ALIPAY_LOGONID");
        map.put("payee_account", account);
        map.put("amount", StrUtil.fen2yuan(amount));
        map.put("payer_show_name", "mfoyou提现");
        if (name != null)
            map.put("payee_real_name", name);
        map.put("remark", "mfoyou提现,加入mfoyou,成就创业梦想");
        request.setBizContent(JSON.toJSONString(map));
        AlipayFundTransToaccountTransferResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            logger.error(JSON.toJSONString(map), e);
            e.printStackTrace();
        }
        return response;

    }
}
